package cn.bob.controller;

import cn.bob.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * @author bob
 * @data 2022/11/8-18:21
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    //注入模板
    @Autowired
    private RestTemplate restTemplate;

    //订单调用user表返回user值
    @GetMapping("{id}")
    @HystrixCommand(fallbackMethod = "getUserHystrixCommand")
    public User getUser(@PathVariable("id") Long id){
        //开启负载均衡 开启使用user-server代替他的服务  ribbo默认为轮询
        String url = "http://user-server/user/" + id;
        return restTemplate.getForObject(url,User.class);
    }

    public User getUserHystrixCommand(@PathVariable("id") Long id){
        return new User(-1L,"触发了熔断",22,"服务不可用请稍后重试！！");
    }
}
